package com.yjhb.android.sbz.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.MainActivity;
import com.yjhb.android.sbz.activity.PostersActivity;
import com.yjhb.android.sbz.activity.PromotionFriendsActivity;
import com.yjhb.android.sbz.activity.rule.RuleInvitationActivity;
import com.yjhb.android.sbz.tool.SharePopup;
import com.yjhb.android.sbz.tool.ShareTool;
import com.yjhb.android.sbz.tool.Tool;
import com.yjhb.android.sbz.v2.activity.PromotionRewardActivityV2;
import com.yjhb.android.sbz.v2.activity.V2InviteFriendActivity;
import com.yjhb.android.sbz.v2.dialog.InvitationRewardDialog;
import com.yjhb.android.sbz.v2.dialog.LeaderDialog;
import com.yjhb.android.sbz.v2.mvp.presenter.InvitationPresenter;
import com.yjhb.android.sbz.v2.mvp.view.InvitationView;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxClipboardTool;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/3 14:30
 */
public class InvitationFragment extends BaseFragment<InvitationPresenter> implements InvitationView, DialogInterface.OnDismissListener {
    private final String TAG = "InvitationFragment";


    @BindView(R.id.QRCode_iv)
    FrameLayout QRCode_iv;
    @BindView(R.id.clickanim_iv)
    ImageView clickanim_iv;
    @BindView(R.id.back)
    View back;
    @BindView(R.id.reward_iv)
    View reward_iv;
    @BindView(R.id.invite_code_tv)
    TextView invite_code_tv;
    @BindView(R.id.invite_code_layout)
    View invite_code_layout;
    @BindView(R.id.invite_reward_rule_tv)
    TextView invite_reward_rule_tv;
    @BindView(R.id.invite_reward_rule2_tv)
    TextView invite_reward_rule2_tv;
    @BindView(R.id.invite_reward_tips_tv)
    TextView invite_reward_tips_tv;

    /**
     * 红包是否可领
     */
    private boolean isAvailable = false;
    /**
     * 第一次进入界面
     */
    private boolean isFirst = true;


    @Override
    protected InvitationPresenter initPresenter() {
        return new InvitationPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_invitation;
    }

    @Override
    protected void initUI() {
        try {
            final AnimationDrawable animationDrawable = (AnimationDrawable) clickanim_iv.getBackground();
            animationDrawable.start();
            if (getContext() instanceof MainActivity) {
                back.setVisibility(View.GONE);
            } else if (getContext() instanceof V2InviteFriendActivity) {
                back.setVisibility(View.VISIBLE);
            }
            if (App.userInfo != null) {
                invite_code_layout.setVisibility(View.VISIBLE);
                CharSequence code = "我的邀请码是：" + App.userInfo.getId();
                invite_code_tv.setText(ZxSpanTool.ColorSpan(code, 7, code.length(), Color.parseColor("#FFF60D")));
            } else {
                invite_code_layout.setVisibility(View.GONE);
            }
            String invite_rule = "1、一级好友完成新手福利，奖励您9元\n" +
                    "2、一级好友完成首次提现，奖励您2元\n" +
                    "3、二级好友完成首次提现，奖励您1元\n" +
                    "4、永久任务分成：一级8%，二级4%\n" +
                    "5、游戏试玩分成：一级20%，二级10%\n" +
                    "6、巨额推广阶梯奖励（详情见下方奖励四：查看榜单）";
            invite_reward_rule_tv.setText(ZxSpanTool.getHighlightSpan(invite_rule, 0xFFFF2C26,
                    "9元", "2元", "1元", "一级8%", "二级4%", "一级20%", "二级10%"));

            CharSequence invite_rule2 = "本自然月推荐100个以上活跃用户，并且仍有能力推荐更多，可申请“掌门人”," +
                    "与赏帮深入合作：永久分成、多级奖励、万元十万元活动奖励，应有尽有！";

            //可点击
            invite_rule2 = ZxSpanTool.getClickSpan(invite_rule2, new ZxSpanTool.OnTextClick() {
                @Override
                public void onTextClick(String text, int startIndex) {
                    LeaderDialog leaderDialog = new LeaderDialog(InvitationFragment.this);
                    leaderDialog.show();
                }
            }, "掌门人");
            //高亮
            invite_rule2 = ZxSpanTool.getHighlightSpan(invite_rule2, 0xFF299EE3, "掌门人");
            //放大
            invite_rule2 = ZxSpanTool.getZoomSpan(invite_rule2, 1.35f, "掌门人");
            invite_reward_rule2_tv.setMovementMethod(new LinkMovementMethod());
            invite_reward_rule2_tv.setText(invite_rule2);

            CharSequence tips = "一人多号等违规作弊将封号处理，具体请看《邀请规则》";

            tips = ZxSpanTool.getClickSpan(tips, new ZxSpanTool.OnTextClick() {
                @Override
                public void onTextClick(String text, int startIndex) {
                    ZxActivityTool.skipActivity(getActivity(), RuleInvitationActivity.class);
                }
            }, "《邀请规则》");
            tips = ZxSpanTool.getHighlightSpan(tips, 0xFFFF2C26, "《邀请规则》");
            invite_reward_tips_tv.setMovementMethod(new LinkMovementMethod());
            invite_reward_tips_tv.setText(tips);

        } catch (Exception e) {
            ZxLogTool.err(e);
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        reward_iv.setVisibility(View.GONE);
        presenter.newUserReward();
    }

    @OnClick(R.id.QRCode_iv)
    void QRCode() {
        if (!Tool.checkLogin(getContext())) {
            return;
        }
        Intent intent = new Intent(getContext(), PostersActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.title_right_tv)
    void title_right_tv() {
        ZxActivityTool.skipActivity(getActivity(), RuleInvitationActivity.class);
    }

    @OnClick(R.id.myPromotionFriend)
    void myPromotionFriend() {
        Intent intent = new Intent(getContext(), PromotionFriendsActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.invite_reward_friend_tv)
    void invite_reward_friend_tv() {
        if (!Tool.checkLogin(getContext())) {
            return;
        }
        SharePopup.showSharePopup2(getActivity(), rootView, 0);
    }

    @OnClick(R.id.promotionReward)
    void promotionReward() {
        ZxActivityTool.skipActivity(getContext(), PromotionRewardActivityV2.class);
    }

    @OnClick(R.id.invi_fission_rank)
    void invi_fission_rank() {
        Intent intent = new Intent(getContext(), PromotionRewardActivityV2.class);
        intent.putExtra("index", 1);
        startActivity(intent);
    }

    @OnClick(R.id.back)
    void back() {
        getActivity().finish();
    }

    @OnClick(R.id.wx)
    void wx() {
        if (!Tool.checkLogin(getContext())) {
            return;
        }
        ShareTool.getInstance().shareImgTextWx(getActivity());
    }

    @OnClick(R.id.pyq)
    void pyq() {
        if (!Tool.checkLogin(getContext())) {
            return;
        }
        ShareTool.getInstance().shareImgTextPyq(getActivity());
    }

    @OnClick(R.id.qq)
    void qq() {
        if (!Tool.checkLogin(getContext())) {
            return;
        }
        ShareTool.getInstance().shareImgTextQQ(getActivity());
    }

    @OnClick(R.id.shareLink)
    void shareLink() {
        ShareTool.getInstance().shareLink();
        ZxToast.showINFOBottomToast("链接已复制到剪切板，快去分享给小伙伴吧~~");
    }

    @OnClick(R.id.reward_iv)
    void reward_iv() {
        if (isAvailable) {
            InvitationRewardDialog dialog = new InvitationRewardDialog(this, 2);
            dialog.setOnDismissListener(this);
            dialog.show();
        } else {
            InvitationRewardDialog dialog = new InvitationRewardDialog(this, 1);
            dialog.setOnDismissListener(this);
            dialog.show();
        }

    }

    @OnClick(R.id.copy_tv)
    void onCopy() {
        if (App.userInfo != null) {
            ZxClipboardTool.copyText(getContext(), App.userInfo.getId());
            ZxToast.showBottomToast("已复制");
        }
    }

    /**
     * 按钮动画
     */
    private void btnAnim() {
        ScaleAnimation scaleAnimation1 = new ScaleAnimation(0.9f, 1.1f, 0.9f, 1.1f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);

        scaleAnimation1.setDuration(1000);
        //重复缩放
        scaleAnimation1.setRepeatMode(Animation.REVERSE);
        //无限循环
        scaleAnimation1.setRepeatCount(-1);
        if (reward_iv != null) {
            reward_iv.setAnimation(scaleAnimation1);
        }
    }


    @Override
    public void onSuccess(boolean b) {
        isAvailable = b;
        if (reward_iv != null) {
            reward_iv.setVisibility(View.VISIBLE);
            btnAnim();
        }
        if (isFirst) {
            isFirst = false;
            if (isAvailable) {
                InvitationRewardDialog dialog = new InvitationRewardDialog(this, 2);
                dialog.setOnDismissListener(this);
                dialog.show();
            } else {
                InvitationRewardDialog dialog = new InvitationRewardDialog(this, 1);
                dialog.setOnDismissListener(this);
                dialog.show();
            }
        }

    }

    @Override
    public void onFailure(int code, String msg) {

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        //更新红包可领状态信息
        presenter.newUserReward();
    }
}
